﻿<MyControl:SelectableItemControl x:Class="UmlControlLibrary.UmlDesignerElement"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MyControl="clr-namespace:UmlControlLibrary"
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Stretch" 
    RenderOptions.CachingHint="Cache"
    x:Name="UmlControlLibraryUmlDesignerElement_xaml" >

    
    <MyControl:SelectableItemControl.Resources>
        <!-- Template used for Properties and Methods Expanders (see Style ItemsControlStyle_Properties) -->
        <ControlTemplate TargetType="{x:Type ItemsControl}" x:Key="ItemsControlTemplate_Properties">
            <!-- <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="False" MaxHeight="150" HorizontalAlignment="Stretch" Focusable="False" MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ScrollViewer}},Path=ViewportWidth}" > -->
            <!-- Stack panel used for hosting Properties or Methods controls -->
            <StackPanel Orientation="Vertical" x:Name="stack_Properties" IsItemsHost="True" ClipToBounds="False" Focusable="False" />
            <!--/ScrollViewer -->
        </ControlTemplate>

        <!-- Template used for Properties and Methods items -->
        <DataTemplate x:Key="PropertiesDataTemplate">
            <MyControl:DraggableLabel Selectable="True" Text="{Binding ElementLabel, Mode=TwoWay}" FontStyle="{Binding FontStyle}" />
            <!--ContextMenu="{StaticResource Property_ContextMenu}"-->
        </DataTemplate>

        <DataTemplate x:Key="SectionHeaderDataTemplate">
            <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Background="Wheat">
                <!--Line Stretch="Fill" Stroke="Black" StrokeThickness="0.5"  VerticalAlignment="Top" X2="30" /-->
                <TextBlock Name="Stereotype" Text="{Binding Mode=OneWay}" VerticalAlignment="Top" HorizontalAlignment="Stretch"  FontStyle="Italic" />
                <!--Line Stretch="Fill" Stroke="Black" StrokeThickness="0.5"  VerticalAlignment="Top" X2="30" /-->
            </StackPanel>
        </DataTemplate>


        <MyControl:PropertyTemplateSelector x:Key="PropertyTemplateSelector" 
                  PropertyTemplate="{StaticResource PropertiesDataTemplate}" 
                  SectionHeaderTemplate="{StaticResource SectionHeaderDataTemplate}" />



        <!-- Style used for expander contents (Properties and Methods) -->
        <Style TargetType="{x:Type ItemsControl}" x:Key="ItemsControlStyle_Properties">
            <Setter Property="Template" Value="{StaticResource ItemsControlTemplate_Properties}"/>
            <Setter Property="ItemTemplateSelector" Value="{StaticResource PropertyTemplateSelector}" />
        </Style>

        <DataTemplate x:Key="SectionDataTemplate">
            <StackPanel>
                <Line Stroke="Black" StrokeThickness="2" Stretch="Fill" VerticalAlignment="Top" X2="30" />
                <Expander Header="{Binding Path=Title}" 
                          IsEnabled="True" 
                          IsExpanded="True" 
                          HorizontalAlignment="Stretch" 
                          VerticalAlignment="Stretch" 
                          Focusable="False"
                          Background="Transparent"
                          Collapsed="expMenu_Collapsed"
                          Expanded="expMenu_Expanded"
                          
                          >
                    <ItemsControl IsTabStop="False"
                                  Initialized="ItemsControl_Initialized" Style="{StaticResource ItemsControlStyle_Properties}"
                                  ContextMenuOpening="ItemsControl_ContextMenuOpening"
                                  Focusable="False"
                                  >
                        <!--ItemsControl.Style>
                            <Style TargetType="ItemsControl">
                                <Setter Property="Template" Value="{StaticResource ItemsControlTemplate_Properties}"/>
                                <Setter Property="ItemTemplateSelector" Value="{StaticResource PropertyTemplateSelector}"/>
                            </Style>
                        </ItemsControl.Style-->
                    </ItemsControl>
                </Expander>
            </StackPanel>
        </DataTemplate>

        <ControlTemplate TargetType="{x:Type ItemsControl}" x:Key="ItemsControlTemplate_Sections">
            <StackPanel Orientation="Vertical" x:Name="stack_Sections" IsItemsHost="True" ClipToBounds="False" Focusable="False" />
        </ControlTemplate>

        <!-- Style used for expander contents (Properties and Methods) -->
        <Style TargetType="{x:Type ItemsControl}" x:Key="ItemsControlStyle_Sections">
            <Setter Property="Template" Value="{StaticResource ItemsControlTemplate_Sections}"/>
            <Setter Property="ItemTemplate" Value="{StaticResource SectionDataTemplate}" />
        </Style>

    </MyControl:SelectableItemControl.Resources>

    <MyControl:ShadowChrome Margin="-2,-2,6,6">
        <Border Margin="10,10,6,6">
            <!-- Uml class control definition -->
            <Grid Name="UmlClassData" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGridLines="False" MaxWidth="Infinity" Focusable="False" SizeChanged="UmlClassData_SizeChanged">
                <!-- Class border -->
                <Rectangle Name="BackgroundRectangle" Stroke="Black" RadiusX="3" RadiusY="3" Margin="-6" OverridesDefaultStyle="False" Grid.RowSpan="3">
                    <!-- Gradient filling. Colors should be binded for customization-->
                    <!--Rectangle.Effect>
                        <DropShadowEffect />
                    </Rectangle.Effect-->
                    <Rectangle.Fill>
                        <LinearGradientBrush>
                            <GradientStop Color="{Binding ElementName=UmlControlLibraryUmlDesignerElement_xaml, Path=GradientStart, Mode=TwoWay}" />
                            <GradientStop Color="{Binding ElementName=UmlControlLibraryUmlDesignerElement_xaml, Path=GradientEnd, Mode=TwoWay}" Offset="1" />
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                    <!--Rectangle.BitmapEffect>
                <DropShadowBitmapEffect />
            </Rectangle.BitmapEffect-->
                </Rectangle>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <!-- sections container -->
                    <StackPanel HorizontalAlignment="Stretch" Grid.Row="0">
                        <TextBlock Name="Stereotype" Text="{Binding Path=Stereotype}" VerticalAlignment="Top" HorizontalAlignment="Stretch"  FontStyle="Italic">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=Stereotype}" Value="">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                 </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                        </TextBlock>
                        <!-- Class name -->
                        <Grid Name="NameSection">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Rectangle Name="NameSectionFiller"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="1" Fill="Transparent"/>
                            <MyControl:DraggableLabel Height="28" x:Name="NameLabel" Selectable="False" VerticalAlignment="Top"  Text="{Binding Path=Name , Mode=TwoWay}" FontWeight="Bold" Grid.Column="0" BeforeEdit="NameLabel_BeforeEdit" AfterEdit="NameLabel_AfterEdit"/>
                            <Image Source="../Resources/Images/Locked.png" VerticalAlignment="Top" HorizontalAlignment="Right" Grid.Column="1"  >
                                <Image.Style>
                                    <Style TargetType="Image">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=Locked}" Value="False">
                                                <Setter Property="Visibility" Value="Collapsed" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Image.Style>
                            </Image>
                        </Grid>
                        <!-- Parent class -->
                        <TextBlock FontSize="9" VerticalAlignment="Top" TextDecorations="None">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Path=Parent}" Value="">
                                            <Setter Property="Visibility" Value="Collapsed" />
                                         </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                            <BulletDecorator Width="9">
                                <BulletDecorator.Bullet>
                                    <Image Source="../Resources/Images/Arrow.png"/>
                                </BulletDecorator.Bullet>
                            </BulletDecorator>
                            <TextBlock Name="ParentLabel" Text="{Binding Path=Parent}" TextWrapping="Wrap"/>
                        </TextBlock>
                    </StackPanel>
                    <!-- Properties and Methods are scrollable -->
                    <ScrollViewer Name="SectionsScrollViewer" CanContentScroll="False" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" SnapsToDevicePixels="True" Margin="0 0 0 0" Focusable="False" Grid.Row="1">
                        <!-- Properties and Methods container -->
                        <ItemsControl Name="SectionsList" Style="{StaticResource ItemsControlStyle_Sections}"
                                  IsTabStop="False"
                                  ItemsSource="{Binding ElementName=UmlControlLibraryUmlDesignerElement_xaml, Path=Sections, Mode=TwoWay}"
                                  Focusable="False"/>
                    </ScrollViewer>
                </Grid>
            </Grid>
        </Border>
    </MyControl:ShadowChrome>
</MyControl:SelectableItemControl>
